package com.example.com.hupu.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.com.hupu.R;
import com.example.com.hupu.activity.VideoActivity;
import com.example.com.hupu.bean.NBALive;

import java.util.List;

/**
 * 新闻Adapter
 */
public class NBALiveAdapter extends RecyclerView.Adapter<NBALiveAdapter.ViewHolder> {

    public List<NBALive> mResult;

    public NBALiveAdapter(List<NBALive> result) {
        this.mResult = result;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_match_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final NBALive item = mResult.get(position);

        String matchStatus = "";
        String quarter = item.quarter;

        if ("4".equals(item.liveType)) {
            matchStatus = "已结束";
        } else {
            if ("".equals(quarter)) {
                matchStatus = "未开始";
            } else {
                matchStatus = quarter;
            }
        }

        holder.tvLeftTeam.setText(item.leftName);
        holder.tvLeftTeamPoint.setText(item.leftGoal);
        holder.tvRightTeam.setText(item.rightName);
        holder.tvRightTeamPoint.setText(item.rightGoal);
        holder.tvMatchDesc.setText(item.matchDesc);
        holder.tvMatchStatus.setText(matchStatus);

        Glide.with(holder.ivLeftTeam).load(item.leftBadge).into(holder.ivLeftTeam);
        Glide.with(holder.ivRightTeam).load(item.rightBadge).into(holder.ivRightTeam);

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(holder.ivLeftTeam.getContext(), VideoActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                holder.mView.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mResult == null ? 0 : mResult.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View mView;
        TextView tvMatchStatus;
        ImageView ivLeftTeam;
        TextView tvLeftTeam;
        TextView tvLeftTeamPoint;
        TextView tvRightTeamPoint;
        TextView tvMatchDesc;
        ImageView ivRightTeam;
        TextView tvRightTeam;
        LinearLayout llMatchList;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            tvMatchStatus = view.findViewById(R.id.tv_match_status);
            ivLeftTeam = view.findViewById(R.id.iv_left_team);
            tvLeftTeam = view.findViewById(R.id.tv_left_team);
            tvLeftTeamPoint = view.findViewById(R.id.tv_left_team_point);
            tvRightTeamPoint = view.findViewById(R.id.tv_right_team_point);
            tvMatchDesc = view.findViewById(R.id.tv_match_desc);
            ivRightTeam = view.findViewById(R.id.iv_right_team);
            tvRightTeam = view.findViewById(R.id.tv_right_team);
            llMatchList = view.findViewById(R.id.ll_match_list);
        }
    }

}